//
//  XMUserManager.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/9.
//

#import "XMUserManager.h"

@implementation XMUserManager

static XMUserManager *manager;
static dispatch_once_t onceToken;
+ (instancetype)shareInstance {
    if (!manager) {
        dispatch_once(&onceToken, ^{
            manager = [[XMUserManager alloc] init];
        });
    }
    return manager;
}

- (void)saveUserModel:(XMUserModel *)userModel {
    XMUserModel *user1 = [self getUser];
    if ([user1.openid isEqualToString:userModel.openid]) {
        user1.accessToken = userModel.accessToken;
        user1.refreshToken = userModel.refreshToken;
    }
    if (userModel) {
        [NSKeyedArchiver archiveRootObject:userModel toFile:kUserPath];
    }
    else {
        [self removeUser];
    }
}

- (XMUserModel *)getUser {
    XMUserModel *user = [NSKeyedUnarchiver unarchiveObjectWithFile:kUserPath];
    return user;
}

- (void)removeUser {
    NSError *error = nil;
    [[NSFileManager defaultManager] removeItemAtPath:kUserPath error:&error];
    if (error) {
//        DLog(@"%@",error.description);
    }
}

@end
